//
//  MainTabBarController.m
//  OSClient
//
//  Created by Chaosky on 15/10/19.
//  Copyright © 2015年 1000phone. All rights reserved.
//

#import "MainTabBarController.h"
#import "OSCBaseViewController.h"
#import <RESideMenu.h>
#import "OptionButton.h"
#import "OSCNewsViewController.h"

@interface MainTabBarController ()

@end

@implementation MainTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupUI];
    [self addTabBarItems];
    [self addNavigationItems];
    [self addCenterButtonWithImage:@"tabbar-more"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

// 设置UI
- (void)setupUI
{
    // 创建视图控制器
    
    // 综合视图控制器
    
    NSMutableArray * controllers = [[NSMutableArray alloc] init];
    for (int index = 0; index < 4; index++) {
        OSCNewsViewController * vc = [[OSCNewsViewController alloc] initWithNewsListType:index];
        vc.view.backgroundColor = [UIColor colorWithRed:(200.f+index*20)/255 green:100.f/255 blue:200.f/255 alpha:1];
        [controllers addObject:vc];
    }
    
    OSCBaseViewController * newsVC = [[OSCBaseViewController alloc] initWithTitle:@"综合" andSubTitles:@[@"资讯", @"热点", @"博客", @"推荐"] andViewControllers:controllers];
    
    UINavigationController * newsNC = [[UINavigationController alloc] initWithRootViewController:newsVC];
    newsNC.navigationBar.translucent = YES;
    
    NSMutableArray * viewControllers = [[NSMutableArray alloc] init];
    
    [viewControllers addObject:newsNC];
    
    for (int index = 0; index < 4; index++) {
        UIViewController * baseVC = [[UIViewController alloc] init];
        UINavigationController * navi = [[UINavigationController alloc] initWithRootViewController:baseVC];
        [viewControllers addObject:navi];
    }
    self.viewControllers = viewControllers;
}

// 设置视图控制器对应的UITabBarItem
- (void)addTabBarItems
{
    // 获取所有的UITabBarItems
    NSArray * items = self.tabBar.items;
    
    // 定制UITabBarItem
    NSArray * itemsName = @[@"综合", @"动弹", @"", @"发现", @"我"];
    NSArray * itemsImage = @[@"tabbar-news", @"tabbar-tweet", @"", @"tabbar-discover", @"tabbar-me"];
    // 遍历数组，另外一种遍历数组的方式，通过block，和(for in)实现的效果是一致的
    [items enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL * stop) {
        UITabBarItem * item = (id)obj;
        item.title = itemsName[idx];
        item.image = [UIImage imageNamed:itemsImage[idx]];
        item.selectedImage = [UIImage imageNamed:[NSString stringWithFormat:@"%@-selected", itemsImage[idx]]];
    }];
}

// 设置导航条
- (void)addNavigationItems
{
    // 取出所有的视图控制器
    NSArray * viewControllers = self.viewControllers;
    
    for (UINavigationController * navi in viewControllers) {
        // 导航视图控制器中存储的视图控制器是放在一个数组中，当pop时将数组末尾的数据移除，相当于是一个stack（栈）
        UIViewController * vc = navi.viewControllers[0];
        // 设置左边和右边的UIBarButtonItem
        vc.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"navigationbar-sidebar"] style:UIBarButtonItemStylePlain target:self action:@selector(sideBarOnClicked:)];
        vc.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(searchOnClicked:)];
    }
}

- (void)sideBarOnClicked:(UIBarButtonItem *) item
{
    // 显示侧边栏
    [self presentLeftMenuViewController:nil];
}

- (void)searchOnClicked:(UIBarButtonItem *) item
{
    
}

// 添加中间的图片
- (void)addCenterButtonWithImage:(NSString *)imageName
{
    // 将中间的UITabBarItem禁用，去创建一个UIButton来替代
    UITabBarItem * secondItem = self.tabBar.items[2];
    secondItem.enabled = NO;
    
    // 创建按钮
    UIButton * centerButton = [UIButton buttonWithType:UIButtonTypeCustom];
    // 获取中间位置
    // 将某个视图上的某个点的位置对应到某个UIView上
    // 【center frame bounds】
    // center：相对于父类而言，该视图中心点所在的位置
    // frame：相对于父类而言，该视图在父视图中所处的位置
    // bounds：就它自身而言，影响的是子视图
    CGPoint center = [self.view convertPoint:self.tabBar.center toView:self.tabBar];
    
    
    NSLog(@"center：%@, frame： %@, bounds：%@", NSStringFromCGPoint(self.tabBar.center), NSStringFromCGRect(self.tabBar.frame), NSStringFromCGRect(self.tabBar.bounds));
    
    // 获取按钮对应的宽度
    CGFloat width = self.tabBar.frame.size.width / 5 - 8;
    CGFloat height = self.tabBar.frame.size.height - 4;
    
    centerButton.frame = CGRectMake(center.x - width / 2 - 4, center.y - height / 2 + 2, width, height);
    
    [centerButton setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    centerButton.backgroundColor = [UIColor blueColor];
    
    // 添加事件
    [centerButton addTarget:self action:@selector(centerButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.tabBar addSubview:centerButton];
}

// 中间按钮被点击
- (void)centerButtonClicked:(UIButton *) button
{
    [self createMoreButtons];
}



// 创建弹出层，显示可以更多的选项
- (void)createMoreButtons
{
    // 创建背景，和当前屏幕大小一样
    UIView * dissView = [[UIView alloc] initWithFrame:self.view.bounds];
    // 设置模糊效果
    dissView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
    //dissView.alpha = 0.4;
    
    // 添加手势
    UITapGestureRecognizer * dissViewTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onDissViewTap:)];
    [dissView addGestureRecognizer:dissViewTap];
    
    [self.view addSubview:dissView];
    
    // 获取屏幕大小
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    
    // 创建contnerView，添加OptionButton
    UIView * contnerView = [[UIView alloc] initWithFrame:CGRectMake(0, screenSize.height - 300, screenSize.width, 300)];
    contnerView.backgroundColor = [UIColor grayColor];
    [dissView addSubview:contnerView];
    
    // 创建OptionButton
    NSArray * buttonNames = @[@"文字", @"相册", @"拍照", @"录音", @"扫一扫", @"找人"];
    NSArray * buttonImages = @[@"tweetEditing", @"picture", @"shooting", @"sound", @"scan", @"search"];
    for (int index = 0; index < 6; index++) {
        OptionButton * button = [[OptionButton alloc] initWithFrame:CGRectMake(index % 3 *(screenSize.width / 3), index / 3 * 120 + 20, screenSize.width / 3,  120) title:buttonNames[index] image:buttonImages[index]];
        [contnerView addSubview:button];
    }
}

// 背景图片点击
- (void)onDissViewTap:(UITapGestureRecognizer *) gesture
{
    [gesture.view removeFromSuperview];
}

@end






